package com.niit.ordersys.order;

import com.niit.ordersys.pay.PaymentResult;
import com.niit.ordersys.pay.PaymentService;

public class OrderProcessor {

    // 引入支付服务
    private PaymentService paymentService;

    /**
     * 构造函数，初始化订单处理器
     * @param paymentService 支付服务实例
     */
    public OrderProcessor(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    /**
     * 处理订单方法
     * @param orderId 订单ID
     * @param amount 订单金额
     * @return OrderResult 订单处理结果
     */
    public OrderResult processOrder(String orderId, double amount) {
        // 验证金额是否合法
        if (amount <= 0) {
            return new OrderResult("failed", null, "金额错误");
        }

        // 调用支付服务进行支付处理
        PaymentResult paymentResult = paymentService.processPayment(orderId, amount);

        // 根据支付结果返回相应的订单处理结果
        if (paymentResult.isSuccess()) {
            return new OrderResult("success", orderId, null);
        } else {
            return new OrderResult("failed", null, "payment_failed");
        }
    }
}
